import axios from 'axios';
import Tools from "./tools";
//import Ext from "./ext";
import UserInfo from "./user-info";
import UserModule from "./user-module";
var AppFrame = {};
AppFrame.Tools = Tools;
//AppFrame.Ext = ext;

AppFrame.UserInfo = UserInfo;
AppFrame.UserModule = UserModule;
AppFrame.Http = {
    Post: function(url, postData, cb) {
        //-----------计算PostVK----------------------
        var PostParamList = [];
        for (var key in postData) {
           if(postData[key]!=undefined){
            var up = {
                key: key.toLocaleLowerCase(),
                value: postData[key]
            };
            PostParamList.push(up);
           }
        };
        PostParamList = PostParamList.sort(function(o, p) {
            var a, b;
            if (typeof o === "object" && typeof p === "object" && o && p) {
                a = o["key"];
                b = p["key"];
                if (a.toLocaleLowerCase() === b.toLocaleLowerCase()) {
                    return 0;
                }
                if (typeof a === typeof b) {
                    return a < b ? -1 : 1;
                }
                return typeof a < typeof b ? -1 : 1;
            }
        });

        var mt= new Date().getTime();
        var PostParamVKStr ="" + mt;

        PostParamList.forEach(function(item){
            PostParamVKStr += item.key + item.value;
        });
        //console.log(PostParamVKStr);
        var VK = Tools.SHA1(PostParamVKStr);

        var instance = axios.create({
            baseURL: Config.WebSiteApiUrl,
            timeout: 5000,
            headers: {'UserID': UserInfo.UserID(),'Token':UserInfo.AuthToken(), 'MT':mt,'VK':VK ,'PartnersId':UserInfo.UserPartnersID(),'UserOrgStructID':UserInfo.UserOrgStructID(), 'ModuleID':UserModule.GetCurSelectModuleID()}
        });

        var Do = function(cb) {

            instance.post(url, postData).then(function(ret) {
                //alert(JSON.stringify(ret));
                if (cb) cb(null, ret.data);
            }).catch(function(err) {
                console.log(url);
                console.log(err);
                if (cb) cb(err);
            })
        }
        // if (cb) {
        //     Do(cb);
        // } else {
        //     return instance.post(url, postData);
        // }

        if (cb) {
            Do(cb);
        } else {
            return new Promise(function(resolve, reject) {
                Do(function(err, ret) {
                    if (err) {
                        console.log(url);
                        console.log(err);
                        reject(err);
                    } else {
                        resolve(ret);
                    }
                })
            })
        }

    },
    Get: function(url, queryData) {

        var UrlParams = AppFrame.Tools.GetUrlParams(url);
        var GetParamList = [];
        for (var key in UrlParams) {
            if(postData[key]!=undefined){
                var up = {
                    key: key.toLocaleLowerCase(),
                    value: UrlParams[key]
                };
            }

            GetParamList.push(up);
        }
        GetParamList = GetParamList.sort(function(o, p) {
            var a, b;
            if (typeof o === "object" && typeof p === "object" && o && p) {
                a = o["key"];
                b = p["key"];
                if (a.toLocaleLowerCase() === b.toLocaleLowerCase()) {
                    return 0;
                }
                if (typeof a === typeof b) {
                    return a < b ? -1 : 1;
                }
                return typeof a < typeof b ? -1 : 1;
            }
        });
        var mt= new Date().getTime();
        var GetParamVKStr = ""+mt;
        GetParamList.forEach(function(item){
            GetParamVKStr += item[i].key + item[i].value;
        });


        var VK = Tools.SHA1(GetParamVKStr);

        var instance = axios.create({
            baseURL: Config.WebSiteApiUrl,
            timeout: 5000,
            headers: {'UserID': UserInfo.UserID(),'Token':UserInfo.UserAccessToken(), 'MT':mt,'VK':VK ,'PartnersId':UserInfo.UserPartnersID(),'UserOrgStructID':UserInfo.UserOrgStructID(), 'ModuleID':UserModule.GetCurSelectModuleID()}
        });
        var Do = function(cb) {
            instance.get(url, postData).then(function(ret) {
                if (cb) cb(null, ret.data);
            }).catch(function(err) {
                console.log(err);
                if (cb) cb(err);
            })
        }
        if (cb) {
            Do(cb);
        } else {
            return new Promise(function(resolve, reject) {
                Do(function(err, ret) {
                    if (err) {
                        console.log(err);
                        reject(err);
                    } else {
                       // console.log(ret);
                        resolve(ret);
                    }
                })
            })
        }
    }
}

AppFrame.Socket = function() {
    var Client = null;
    var getClient = function() {
        return Client;
    };

    var CreateWebSocket = function(UserID) {

        var SocketServerHost =window.location.protocol+ "//" + window.location.host + "/socket/manage";
        console.log(SocketServerHost)
        Client = io.connect(SocketServerHost);
        //开始连接服务器
        Client.on('connect', function() {});

        Client.on('ConnectionState', function(data) {
            //console.log(data);
            if (data.ConnectionState == 1) {
                var ClientInfo = {
                    UserID: UserID
                };

                Client.emit("SetClientInfo", ClientInfo, function(backData) {
                    if (backData && backData.Result == 1) {
                        console.log("客户端连接成功");
                    }
                });
            }
        });

        Client.on('ReceiveMsg', function(data) {
console.log('ReceiveMsg',data);
            $(document).triggerHandler("SocketReceiveMsg", data);
        });

        Client.on('disconnect', function(data) {

        })

        Client.on('reconnect', function() {

        })
    };

    var Connect = function() {
        Client.socket.reconnect();
    };

    var DisConnect = function() {
        Client.disconnect();
    };

    return {
        CreateWebSocket: CreateWebSocket,
        Connect: Connect,
        DisConnect: DisConnect,
        getClient: getClient
    };
}();


export default AppFrame;
